﻿module poseidon.controller.debugcontrol.debugPanel;


private import dwt.all;

private import poseidon.controller.editor;
private import poseidon.controller.gui;
private import poseidon.globals;
private import poseidon.i18n.itranslatable;


class TopPanel : CTabFolder
{
	int[]		TopPanelLastWeight, lastSashWeights;
	SashForm	_parent;
	const int	TAB_HEIGHT = 24;
	Composite	tbContainer;	// tool bar container
	
	CDebugItem	debugItem;
	StackLayout	stackLayout;

	this( SashForm parent )
	{
		super( parent, DWT.TOP | DWT.BORDER );
		_parent = parent;
		
		initGUI();
	}
	
	private void initGUI()
	{
		//setMinimizeVisible(true);
		Color[3] 	colorsActive = [DWTResourceManager.getColor(0, 100, 255), DWTResourceManager.getColor(113, 166, 244), null ];
		this.setSelectionBackground( colorsActive, [60, 100], true);
		this.setTabHeight(24);
		this.setSimple(false);
		this.setSelectionForeground(DWTResourceManager.getColor(255,255,255));

		debugItem = new CDebugItem(this);
		this.setSelection(0);
		tbContainer = new Composite(this, DWT.NONE);
		stackLayout = new StackLayout();
		tbContainer.setLayout(stackLayout);
		
		Control top = debugItem.getTbBar(tbContainer);
		stackLayout.topControl = top;
		tbContainer.layout();
		setTopRight(tbContainer);
	}
}


class CDebugItem : CTabItem, ITranslatable
{
private:
	import 		poseidon.controller.debugcontrol.debugger;

	
	Composite 	tbBar = null;
	ToolItem	tiResume, tiTerminate, tiCommand, tiIn, tiOver, tiReturn;//, tiSuspend;

	void initGUI( Composite parent )
	{
		setImage( Globals.getImage( "debug_exc" ) );

		content = new Text( parent, DWT.MULTI | DWT.WRAP | DWT.V_SCROLL );
		scope font = new Font( display, Editor.settings._setting.outputStyle.font, Editor.settings._setting.outputStyle.size, DWT.NORMAL );
		content.setFont( font );
		this.setControl( content );
	}

public:
	Text		content;
	Text		txtCommandLine;
	
	this( CTabFolder parent )
	{
		super( parent, DWT.NONE );
		initGUI( parent );
		updateI18N();
		//content.handleEvent( content, DWT.MouseDoubleClick, &onDBClick );
	}

	Control getTbBar( Composite container )
	{
		if( tbBar is null)
		{
			tbBar = new Composite( container, DWT.NONE );
			GridLayout gl = new GridLayout();
			tbBar.setLayout(gl);
			with( gl )
			{
				marginWidth = 0;
				marginHeight = 0;
			}
			ToolBar toolbar = new ToolBar( tbBar, DWT.FLAT | DWT.HORIZONTAL  );
			toolbar.setLayoutData( new GridData( GridData.HORIZONTAL_ALIGN_END ) );


			with( tiResume = new ToolItem( toolbar, DWT.NONE ) )
			{
				setImage( Globals.getImage( "debug_resume" ) );
				setDisabledImage( Globals.getImage( "debug_resume_dis" ) );
				setToolTipText( Globals.getTranslation( "debug.tooltip_run" ) );
				setEnabled( false );
				handleEvent( this, DWT.Selection, delegate( Event e ){
					sActionMan.actionDebugExec(e);
				});
			}
		
			with( tiTerminate = new ToolItem( toolbar, DWT.NONE ) )
			{
				setImage( Globals.getImage( "progress_stop" ) );
				setDisabledImage( Globals.getImage( "progress_stop_dis" ) );
				setToolTipText( Globals.getTranslation( "debug.tooltip_stop" ) );
				setEnabled( false );
				handleEvent( this, DWT.Selection, delegate( Event e ){
					sActionMan.actionDebugStop(e);
				});
			}

			
			new ToolItem( toolbar, DWT.SEPARATOR );
			ToolItem mother = new ToolItem(toolbar, DWT.SEPARATOR);

			with( txtCommandLine = new Text( toolbar, DWT.BORDER ) )
			{
				mother.setWidth( 120 );
				mother.setControl( txtCommandLine );
				setEnabled( false );
				scope font = new Font( display, "Arial", 8, DWT.NORMAL );
				setFont( font );
				
				handleEvent( this, DWT.KeyDown, delegate( Event e )
				{
					if( e.keyCode == DWT.CR || e.keyCode == DWT.KEYPAD_CR  )
					{
						sActionMan.actionDebugCommand(e);
					}
				});
			}			

			with( tiCommand = new ToolItem( toolbar, DWT.NONE ) )
			{
				setImage( Globals.getImage( "debug_command" ) );
				setDisabledImage( Globals.getImage( "debug_command_dis" ) );
				setToolTipText( "Send Console Command" );
				setEnabled( false );
				handleEvent( this, DWT.Selection, delegate( Event e )
				{
					sActionMan.actionDebugCommand(e);
				});
			}
			
			new ToolItem( toolbar, DWT.SEPARATOR );

			with( tiIn = new ToolItem( toolbar, DWT.NONE ) )
			{
				setImage( Globals.getImage( "debug_stepinto" ) );
				setDisabledImage( Globals.getImage( "debug_stepinto_dis" ) );
				setToolTipText( Globals.getTranslation( "debug.tooltip_in" ) );
				setEnabled( false );
				handleEvent( this, DWT.Selection, delegate( Event e ){
					sActionMan.actionDebugStepInto(e);
				});
			}

			with( tiOver = new ToolItem( toolbar, DWT.NONE ) )
			{
				setImage( Globals.getImage( "debug_stepover" ) );
				setDisabledImage( Globals.getImage( "debug_stepover_dis" ) );
				setToolTipText( Globals.getTranslation( "debug.tooltip_over" ) );
				setEnabled( false );
				handleEvent( this, DWT.Selection, delegate( Event e ){
					sActionMan.actionDebugStepOver(e);
				});
			}

			with( tiReturn = new ToolItem( toolbar, DWT.NONE ) )
			{
				setImage( Globals.getImage( "debug_stepreturn" ) );
				setDisabledImage( Globals.getImage( "debug_stepreturn_dis" ) );
				setToolTipText( Globals.getTranslation( "debug.tooltip_ret" ) );
				setEnabled( false );
				handleEvent( this, DWT.Selection, delegate( Event e ){
					sActionMan.actionDebugStepReturn(e);
				});
			}

			new ToolItem( toolbar, DWT.SEPARATOR );

			with( new ToolItem( toolbar, DWT.CHECK ) )
			{
				setImage( Globals.getImage( "synced" ) );
				setToolTipText( Globals.getTranslation( "debug.tooltip_cons" ) );
				setSelection( sGUI.debuggerDMD.bShowOutPut );
				handleEvent( this, DWT.Selection, delegate( Event e )
				{
					sGUI.debuggerDMD.bShowOutPut = !sGUI.debuggerDMD.bShowOutPut;
					setSelection( sGUI.debuggerDMD.bShowOutPut );
					if( !sGUI.debuggerDMD.bShowOutPut ) content.setText( "" );
				});				
			}

			with( new ToolItem( toolbar, DWT.NONE ) )
			{
				setImage(Globals.getImage("close_view"));
				setToolTipText( Globals.getTranslation( "debug.tooltip_cleancons" ) );
				handleEvent(this, DWT.Selection, delegate(Event e) {
					CDebugItem pthis = cast(CDebugItem) e.cData;
					pthis.content.setText( "" );
				});
			}			

			updateToolBar();
		}
		
		return tbBar;
	}
	
	//void setBusy( bool busy){ tiTerminate.setEnabled( busy ); }

	void updateI18N(){ this.setText( Globals.getTranslation( "debug.debug" ) );	}

	void updateToolBar() 
	{
		if( sGUI.debuggerDMD is null ) return;

		if( !sGUI.debuggerDMD.isPipeCreate() )
		{
			tiResume.setEnabled( false );
			tiCommand.setEnabled( false );
			txtCommandLine.setEnabled( false );
			tiTerminate.setEnabled( false );
			tiIn.setEnabled( false );
			tiOver.setEnabled( false );
			tiReturn.setEnabled( false );
		}
		else
		{
			tiResume.setEnabled( true );
			tiTerminate.setEnabled( true );
			tiCommand.setEnabled( true );
			txtCommandLine.setEnabled( true );

			if( sGUI.debuggerDMD.isRunning )
			{
				//tiSuspend.setEnabled( true );
				tiIn.setEnabled( true );
				tiOver.setEnabled( true );
				tiReturn.setEnabled( true );
				
			}
			else
			{
				//tiSuspend.setEnabled( false );
				tiIn.setEnabled( false );
				tiOver.setEnabled( false );
				tiReturn.setEnabled( false );
			}
		}
	}
}